<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="数据集编码" prop="setCode">
        <el-input
          v-model="queryParams.setCode"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据集名称" prop="setName">
        <el-input
          v-model="queryParams.setName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据源编码" prop="sourceCode">
        <el-select
          v-model="queryParams.sourceCode"
          placeholder="请选择数据集类型"
        >
          <el-option
            v-for="(item, index) in sourceCodeDatas"
            :key="index"
            :label="item.sourceName"
            :value="item.sourceCode"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column
        prop="setCode"
        label="数据集编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="setName"
        label="数据集名称"
        align="center"
      ></el-table-column>
      <el-table-column
        label="描述"
        align="center"
        prop="setDesc"
      ></el-table-column>
      <el-table-column
        label="数据源编码"
        align="center"
        prop="sourceCode"
      ></el-table-column>
      <el-table-column
        prop="setType"
        label="数据集类型"
        align="center"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            v-if="scope.row.parentId != 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改部门对话框 -->
    <Edit
      v-if="dialogVisible"
      :dialogTitle="dialogTitle"
      :dialogOpen="dialogOpen"
      :dialogData="dialogData"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script>
import {
  getDatasetLists,
  deleteDataset,
  queryAllDataSource
} from "@/api/aifactory";
import Edit from "./components/edit.vue";

export default {
  name: "Datasource",
  components: { Edit },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      dataList: [],
      // 重新渲染表格状态
      refreshTable: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sourceCode: undefined,
        sourceName: undefined,
      },
      sourceCodeDatas: [],
      //弹窗参数
      dialogVisible: false,
      dialogTitle: "",
      dialogOpen: false,
      dialogData: {},
    };
  },
  created() {
    this.getList();
    //获取数据源
    queryAllDataSource().then((res) => {
      if (res.code == 200) {
        this.sourceCodeDatas = res.data;
        console.log("sourceCodeDatas:", this.sourceCodeDatas);
      }
    });
  },
  methods: {
    getList() {
      this.loading = true;
      getDatasetLists(this.queryParams).then((response) => {
        this.dataList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children,
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //关闭库相关
    closeDialog() {
      this.dialogVisible = false;
      this.dialogOpen = false;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.dialogOpen = true;
      this.dialogTitle = "新建数据源";
      this.dialogVisible = true;
      this.dialogData = {};
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(218, row);
      this.dialogOpen = true;
      this.dialogTitle = "编辑数据源";
      this.dialogVisible = true;
      this.dialogData = row;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(254, row)
      this.$modal
        .confirm('是否确认删除名称为"' + row.setName + '"的数据项？')
        .then(function () {
          return deleteDataset([row.id]);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
